<template>
    <div>
        123
        <div>
            <input type="text" v-model="user.username">
            <input type="text" v-model="user.password">
            <button @click="oprator">{{status === 'login' ? '登录' :'注册'}}</button>
        </div>

        <div>
            <button @click="status='login'">登录</button>
            <button @click="status='registry'">注册</button>
        </div>
    </div>
</template>

<script>
export default {
    data(){
        return {
            status:'login',
            user:{
                username:'',
                password:''
            }
        }
    },
    methods:{
        oprator(){
            let url = this.status === 'login' ? '/api/login' : '/api/registry';
            let {username,password} = this.user;
            if(username && /\d{6,8}/.test(password)){
                this.$http.post(url,{username:this.user.username,password:this.user.password}).then(res => {
                    alert(res.data.msg);
                    if(res.data.code === 1 && this.status === 'login'){
                        localStorage.setItem('username',username);
                        this.$router.push('/home');
                    }
                })
            }else{
                alert('参数有误');
            }
        }
    } 
}
</script>